package com.neusoft.a132wxyorderingdish.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.activity.FoodInfoActivity;
import com.neusoft.a132wxyorderingdish.beans.FoodBean;
import com.neusoft.a132wxyorderingdish.common.Constants;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<FoodBean> foodlist;
    private LayoutInflater layoutInflater;

    public FoodListAdapter(Context context,List<FoodBean> foodlist){
        this.context=context;
        this.foodlist=foodlist;
        layoutInflater= LayoutInflater.from(context);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View v=layoutInflater.from(viewGroup.getContext()).inflate(R.layout.food_item,viewGroup,false);
        ViewHolder holder=new ViewHolder(v);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        final FoodBean food=foodlist.get(position);
        if(null==food)
            return;

        ViewHolder foodViewHolder=(ViewHolder)viewHolder;
        //绑定数据
        foodViewHolder.food_name.setText(food.getFoodname());
        foodViewHolder.food_price.setText(food.getPrice()+"元");
        foodViewHolder.food_intro.setText(food.getIntro());
        Picasso.get().load(Constants.BASE_URL+food.getPic()).into(foodViewHolder.food_pic);

        //点击事件
        foodViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, FoodInfoActivity.class);
                intent.putExtra("food_id",food.getFood_id());

                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if(foodlist==null){
            return 0;
        }
        return  foodlist.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView food_pic;
        public TextView food_name,food_price,food_intro;

        public ViewHolder(View itemView) {
            super(itemView);
            food_intro=itemView.findViewById(R.id.food_intro);
            food_name=itemView.findViewById(R.id.food_name);
            food_price=itemView.findViewById(R.id.food_price);
            food_pic=itemView.findViewById(R.id.food_image);
        }
    }
}
